<?php
namespace AuctionCore\Spm;
use AuctionCore\Spm\AccountManage;
/**
 * 交易记录
 */
use AuctionCore\Account\Back\Employee;

/**
 * 车源列表
 * @author       shijiaibing
 */
class AutionRecord extends \AuctionSpm\Controller\BaseController {
    //初始化
    public function __construct() {           
        parent::__construct();
    }
    
	public function get($ev_page_num, $page) {
        
    }
    public function count() {
        
    }

    /**
     * 获取车源列表
     * @param integer $page
     * @param integer $size
     * @param array $paramer
     * @return multitype:array(list,total)
     */
    public function get_list($order_id) {
        // $map['l.create']
        // $map['c.suggest_max_price'] = array('neq',' ');
        // $map['c.suggest_min_price'] = array('neq',' ');
        
        $user_m = new AccountManage();
        $map1 = $user_m->getFourMap();

        $map['o.order_id'] = array('eq',$order_id);
        $map = array_merge($map,$map1);
        $cols = "c.car_id,c.car_no,c.brand_id,c.series_id,c.model_id,c.car_status,c.reserve_price,c.suggest_min_price,c.suggest_max_price
                ,c.composite_state,c.accident_level,c.re_auction_type,c.re_auction_reason,c.plate_prefix,c.plate_suffix,c.success_price,c.transfer_limit,o.bid_best_price,o.bidding_best_price,o.order_no,o.order_id,o.status as ostatus,o.is_consult,o.first_pay_status,o.bidding_end_time,o.remark_reject,co.owner_id,o.bidding_start_time,c.pointer_mileage,co.status,co.checker_id,co.check_date,l.result";         
        $list = M('cars')->distinct('c.car_id')->alias('c')->field($cols)
        			     ->join(C('DB_PREFIX') . "order o ON c.car_id=o.car_id")
                         ->join(C('DB_PREFIX')."car_owner as co ON co.owner_id=c.owner_id")
        			     // ->join(C('DB_PREFIX')."car_pictures as cp ON cp.car_id=o.car_id")
        			     ->join('LEFT JOIN '.C('DB_PREFIX'). "transfer_log as l ON l.order_id=o.order_id")
                         ->where($map)->order("l.transfer_id desc")->limit(1)->select();
        $cm = new \AuctionCore\CarResource\CarModel();
        // echo M()->getLastSql();
        //1 必须过本地,2 必须过外户,3 不限制
        //
        //数据处理
        foreach ($list as $key => $val) {
            $list[$key] = $val;
            $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
            $series = $cm->get_model_by_id($val['series_id'], 'series');
            $model = $cm->get_model_by_id($val['model_id']);
            $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
            $list[$key]['reserve_price'] = format_money($val['reserve_price']);
            $list[$key]['suggest_min_price'] = format_money($val['suggest_min_price']);
            $list[$key]['suggest_max_price'] = format_money($val['suggest_max_price']);
            $list[$key]['car_plate_number'] = C('PLATE_PREFIX.' . $val['plate_prefix']) . $val['plate_suffix'];
            //拍卖结束时间
            $bidendtime = date($val['bidding_start_time']);
            $list[$key]['bidding_endtime'] = date('Y-m-d',strtotime($bidendtime));
            //左前图片
            $list[$key]['left_front45'] = $this->getPics($val['car_id']);
            //拍卖最高价
            $list[$key]['bid_high_price'] = ($val['bid_best_price'] > $val['bidding_best_price']) ? $val['bid_best_price'] : $val['bidding_best_price'];
            $in_map = array('7','8','9','10','11');
            if($val['car_status'] == '8' || $val['car_status'] == '9'){
                $list[$key]['car_status_cn'] = C('CAR_STATUS.' . $val['car_status']);
            } elseif($val['ostatus'] == 5){
                $list[$key]['car_status_cn'] = C('ORDER_STATUS.' . $val['ostatus']);
            } elseif(in_array($val['ostatus'], array(1,2))){
                $list[$key]['car_status_cn'] = '待上拍';
            } elseif(in_array($val['ostatus'], $in_map)){
                $list[$key]['car_status_cn'] = '交易中';
            } elseif(in_array($val['ostatus'], array(3,301,4))){
                $list[$key]['car_status_cn'] = 拍卖中;
            }
        }
        return $list;
    }
    /**
     * 获取图片
     * @param  [type] $car_id [description]
     * @return [type]         [description]
     */
    public function getPics($car_id){
        $map['car_id'] = $car_id;
        $map['pic_key'] = array('eq','p_left_front45'); 
        $pics = M('car_pictures')->where($map)->select();
        return $pics['pic_url'];
    }
    /**
    * 获得品牌、车系、车型信息
    * @param int $id id
    * @param string $type 类型(mbrand,brand,series,model)
    * @return array
    */
    public function get_brand_by_name($name,$type='brand'){
            $condition['name'] = array('like','%'.$name.'%');
            return M('car_'.$type)->where($condition)->find();
    }   
    public function getParams($paramer){
        $map = array();
         //car：车辆状态(0待上传协议,1 待审核,2 审核失败,3 审核驳回,4 待拍卖,5 拍卖中,6 流拍,7 待重拍, 8 交易失败, 9 交易成功)
        //order：拍单状态(1审核中,2审核驳回,3投标中,301等待竞标,4竟标中,5成效待确认,6拍单流拍,7待签约,8待付首款，
        //801已付首款,9待过户,10过户中,11待付尾款,12拍单失败,13拍单成功
        switch ($paramer['status']) {
            case 1:
                $where['c.car_status'] = array('in', array('9','8'));
                $where['o.status'] = array('in', array('5','7','8','9','10','11'));
                $where['_logic'] = 'OR';
                $map['_complex'] = $where;
                break;
            case '2':
                $map['o.status'] = array('eq', 5);
                break;
            case '3':
                $map['o.status'] = array('in', array('7','8','9','10','11'));
                break;
            case '4':
                $map['c.car_status'] = array('eq', 9);
                break;
            case '5':
                $map['c.car_status'] = array('eq', 8);
                break;
            default:
                break;
        }
        //车辆名称、车牌号、卖家信息 搜索
        if(!empty($paramer['keyword'])){
             $paramer['keyword'] = trim($paramer['keyword']);
             if(preg_match("/[\x7f-\xff]/", $paramer['keyword'])){
                $b_result = $this->get_brand_by_name($paramer['keyword']);
                $b_id = $b_result['brand_id'];
                //完整的车牌号
                $first_code = mb_substr($paramer['keyword'], 0,1,'utf-8');
                $last_code = mb_substr($paramer['keyword'], 1,6,'utf-8');
                $plate_num = array_flip(C('PLATE_PREFIX'));
                $where['c.plate_prefix'] = array('eq',$plate_num[$first_code]);
                $where['c.plate_suffix'] = array('eq',$last_code);
                $where['c.brand_id'] = array('eq',$b_id);
                $where['_logic'] = 'OR';
                $map['_complex'] = $where;
             } else{
                //只有编号
                $map['c.plate_suffix'] = array('eq',$paramer['keyword']);
             }
        }
        //车辆检测时间
        if(!empty($paramer['start_time']) || !empty($paramer['end_time'])){
            if (empty($paramer['start_time'])) {
                $map['co.check_date'] = array("elt", $paramer['end_time'].' 23:59:59');
            } elseif (empty($paramer['end_time'])) {
                $map['co.check_date'] = array("egt", $paramer['start_time'] .' 00:00:00');
            } else {
                $map['co.check_date'] = array(array("egt", $paramer['start_time'] .' 00:00:00'), array("elt", $paramer['end_time'] .' 23:59:59'));
            }
        }
        return $map;
    }
}